package edu.bit.water.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.bit.water.entity.EntityWaterBalaInfo;
import edu.bit.water.entity.LabelWaterBalaInfo;
import edu.bit.water.dao.EntityWaterBalaDao;
import edu.bit.water.dao.LabelWaterBalaDao;
import edu.bit.water.dao.UserDao;

@Controller
@RequestMapping("/label_water_bala")
public class LabelWaterBalaController extends BaseController{
	
	@Autowired
	public LabelWaterBalaDao labelWaterBalaDao;
	
	@Autowired
	public EntityWaterBalaDao entityWaterBalaDao;

	/**
	 * 查询数据
	 * @return 真实数据或是错误信息
	 */
	@ResponseBody
	@RequestMapping(value = "/select", method = RequestMethod.GET)
	public Map<String, Object> queryWaterBala() {
		Map<String, Object> modelMap = new HashMap<>();
		List<LabelWaterBalaInfo> labelWaterBalaList = new ArrayList<LabelWaterBalaInfo>();
		try {
			labelWaterBalaList = labelWaterBalaDao.queryLabelWaterBala();
			modelMap.put("status", "success");
			modelMap.put("rows", labelWaterBalaList);
			
		} catch (Exception e) {
			modelMap.put("status", "failed");
			modelMap.put("msg", e.getMessage());
		}
		return modelMap;
	}
	
	/**
	 * 插入数据
	 * @param labelEvaWsrcInfo
	 * @return 成功或失败信息放入map中
	 */
	@ResponseBody
	@RequestMapping("/insert")
	public Map<String, Object> insertWaterBala(LabelWaterBalaInfo labelWaterBala) {
		Map<String, Object> modelMap = new HashMap<>();
		if(currentUser()==null) {
			modelMap.put("status", "failed");
			return modelMap;
		}
		int userid=currentUser().getId();
		//String username=currentUser().getName();
		labelWaterBala.setUserId(userid);
		//labelWaterBala.setUserName(username);
		

		try {
			int i = labelWaterBalaDao.insertLabelWaterBala(labelWaterBala);
			//创建8张表
			EntityWaterBalaInfo entityWaterBalaInfo=new EntityWaterBalaInfo();
			entityWaterBalaInfo.setCompanyName(labelWaterBala.getCompanyName() );
			entityWaterBalaInfo.setCreatedAt(labelWaterBala.getCreatedAt());
			entityWaterBalaInfo.setTableName("企业取水水源表"); 
			entityWaterBalaInfo.setUrl("entity_eva_wsrc_info.html");
			entityWaterBalaInfo.setUserId(userid);
			//entityWaterBalaInfo.setUserName(username);
			
			LabelWaterBalaInfo tmp=new LabelWaterBalaInfo();
			tmp=labelWaterBalaDao.queryLabelWaterBalaByName(labelWaterBala.getCompanyName());
			if(tmp==null) {
				modelMap.put("status", "failed");
				return modelMap;
			}
			entityWaterBalaInfo.setCompanyId(tmp.getId());
			//创建8张表
			entityWaterBalaDao.insertEntityWaterBala(entityWaterBalaInfo);
			entityWaterBalaInfo.setTableName("企业年用水表"); 
			entityWaterBalaInfo.setUrl("entity_eva_year_wuse_info.html");
			entityWaterBalaDao.insertEntityWaterBala(entityWaterBalaInfo);
			
			entityWaterBalaInfo.setTableName("企业生产情况统计表"); 
			entityWaterBalaInfo.setUrl("entity_evb_produce_info.html");
			entityWaterBalaDao.insertEntityWaterBala(entityWaterBalaInfo);
			
			entityWaterBalaInfo.setTableName("全厂计量水表配备情况表");
			entityWaterBalaInfo.setUrl("entity_eva_water_sensor_info.html");
			entityWaterBalaDao.insertEntityWaterBala(entityWaterBalaInfo);
			
			entityWaterBalaInfo.setTableName("用水单元水平衡测试表"); 
			entityWaterBalaInfo.setUrl("entity_eva_unit_wbal_test.html");
			entityWaterBalaDao.insertEntityWaterBala(entityWaterBalaInfo);
			
			entityWaterBalaInfo.setTableName("企业水平衡测试统计表"); 
			entityWaterBalaInfo.setUrl("entity_eva_wbal_test.html");
			entityWaterBalaDao.insertEntityWaterBala(entityWaterBalaInfo);
			
			entityWaterBalaInfo.setTableName("企业用水分析表"); 
			entityWaterBalaInfo.setUrl("entity_eva_cmp_water_anl.html");
			entityWaterBalaDao.insertEntityWaterBala(entityWaterBalaInfo);
			
			entityWaterBalaInfo.setTableName("水平衡图"); 
			entityWaterBalaInfo.setUrl("label_eva_wbal_chart.html");
			entityWaterBalaDao.insertEntityWaterBala(entityWaterBalaInfo);
			
			modelMap.put("status", "success");
		} catch (DuplicateKeyException e) {
			if (e.getMessage().contains("INFO_LAB")) {
				modelMap.put("status", "failed");
				modelMap.put("msg", "创建日期已存在，请重新输入");
			}
		}
		return modelMap;
	}
	/**
	 * 
	 * @param listId 删除数据组id号
	 * @return 成功或失败信息放入map中
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public Map<String, Object> deleteLabelEvaWsrcInfo(@RequestBody List<Integer> listId){
		Map<String, Object> modelMap = new HashMap<>();
		try {
			for (Integer id : listId) {
				entityWaterBalaDao.deleteEntityWaterBalaByCompanyId(id);// 组号为
				labelWaterBalaDao.deleteLabelWaterBala(id);
			}
			modelMap.put("status", "success");
		} catch (Exception e) {
			e.printStackTrace();
			modelMap.put("status", "failed");
		}
		//entityWaterBalaDao.deleteAll();//删除其所有数据
		return modelMap;
	}
	
	/**
	 * 跟新标签表
     * @param list 所更新的数据
     * @return 操作后的信息
     */
    @RequestMapping("/update")
    @ResponseBody
    public Map<String, Object> updateLabelEvaWsrcInfo(@RequestBody List<Object> list) {
        Map<String, Object> modelMap = new HashMap<>();
        LabelWaterBalaInfo labelWaterBala = new LabelWaterBalaInfo();
        labelWaterBala.setId((int) list.remove(0));// id
        labelWaterBala.setCreatedAt(list.remove(0).toString());//createdAt
        labelWaterBala.setCompanyName(list.remove(0).toString());// companyName
        labelWaterBala.setClass1(list.remove(0).toString());//class1
		labelWaterBala.setClass2(list.remove(0).toString());//class2
        
        try {
        	labelWaterBalaDao.updateLabelWaterBala(labelWaterBala); 
            modelMap.put("status", "success");
        } catch (BadSqlGrammarException e) {
			modelMap.put("status", "failed");
			//modelMap.put("msg", "数据格式出错"+"\n具体报错:"+e.getMessage());
			modelMap.put("msg", "数据格式出错");
		} catch (DuplicateKeyException e) {
			modelMap.put("status", "failed");
			modelMap.put("msg", "违反唯一性约束");
		} catch (Exception e) {
			modelMap.put("status", "failed");
			modelMap.put("msg", "error class:" + e.getClass());
		}
        
        return modelMap;
    }
}
